package org.devocean.groupbuying.web.backend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.codehaus.jackson.map.ObjectMapper;
import org.devocean.groupbuying.domain.CategoriesLocalization;
import org.devocean.groupbuying.domain.Media;
import org.devocean.groupbuying.service.MediaService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/gb-cpanel/media" })
public class MediaController {

	@Autowired
	private MediaService mediaService;

	@RequestMapping(value = "/")
	public Map<String, ? extends Object> list(
			@RequestParam(value = "start", required = false) Integer start,
			@RequestParam(value = "limit", required = false) Integer size) {
		try {
				int sizeNo = size == null ? 10 : size.intValue();
				List<Media> media = mediaService.findMediaEntries(start, sizeNo);
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
	
				for (Media m : media) {
					Map<String, Object> data = new ConcurrentHashMap<String, Object>();
					data.put("mediaID", m.getMediaID());
					data.put("title", m.getTitle());
					data.put("media_file_path", m.getMediaFilePath());
					result.add(data);
	
				}
	
				long total = mediaService.countMedia();
				return RenderMapUtils.getMap(result, total);
		} catch (Exception e) {
			return RenderMapUtils
					.getModelMapError("Error retrieving Medias from database.");
		}
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody
	String upload(Media media, String[] _categoriesProxy) {	
		try {
			if (_categoriesProxy != null)
				for (String cp : _categoriesProxy) {
					CategoriesLocalization cl = new CategoriesLocalization();
					cl.setCategoriesLocalizationID(Integer.parseInt(cp));
					media.getCategoriesLocalizations().add(cl);
				}
			media = mediaService.saveMedia(media);
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			data.put("mediaID", media.getMediaID());
			data.put("title", media.getTitle());
			ObjectMapper mapper = new ObjectMapper();
			return mapper.writeValueAsString(RenderMapUtils.getMap(data));

		} catch (Exception e) {
			return "{success:false}";
		}

	}

	@RequestMapping(value = "/{id}")
	public Map<String, ? extends Object> edit(@PathVariable("id") Integer id) {
		try {

			Media media = mediaService.findMediaById(id);
			
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			data.put("mediaID", media.getMediaID());
			data.put("title", media.getTitle());
			data.put("media", media.getMediaFilePath());
			
			return RenderMapUtils.getMap(data);
		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Medias from database.");
		}
	}

	@RequestMapping(value = "/", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> update(@RequestBody Media media) {
		try {
			
			Media oldMedia = mediaService.findMediaById(media.getMediaID());			
			oldMedia.setTitle(media.getTitle());
			oldMedia.setDescription(media.getDescription());
			mediaService.updateMedia(oldMedia);
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			data.put("mediaID", oldMedia.getMediaID());
			data.put("title", oldMedia.getTitle());
			return RenderMapUtils.getMap(data);
		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error updating media.");
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id) {
		return mediaService.deleteMedia(id) == true ? "{success:true}" : "{success:false}";

	}
	
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public Map<String,Object> search(String searchText) {
		try { 
			 List<Media> media =  mediaService.search(searchText);
			 List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				
				for (Media m : media) {
					Map<String, Object> data = new ConcurrentHashMap<String, Object>();
					data.put("mediaID", m.getMediaID());
					data.put("title", m.getTitle());
					data.put("media_file_path", m.getMediaFilePath());
					result.add(data);
	
				}
	
				long total = result.size();
				return RenderMapUtils.getMap(result, total);
			 			
		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("No data found!");

		}	
	}

}
